import {KintoneRestAPIClient} from '@kintone/rest-api-client';
import {AppElementInfo} from '../bean/AppElementInfo';
const kintoneClient = new KintoneRestAPIClient();
class AppHttpRequestUtils {
  static getApp(appId) {
    return kintoneClient.app.getApp({id: appId});
  }
  static getAppFields(appId) {
    return kintoneClient.app.getFormFields({app: appId});
  }
  static getAppElementInfo(appId) {
    return new Promise((resolve, reject) =>{
      Promise.all([this.getApp(appId), this.getAppFields(appId)]).then(resp =>{
        resolve(new AppElementInfo(appId, resp[0].name, resp[1].properties));
      }).catch(error =>{
        console.log(error);
        reject(error);
      });
    });
  }
  static getAppDatas(appId, query = '') {
    return kintoneClient.record.getAllRecords({app: appId, query: query});
  }
  static updateAppRecord(appId, recordId, record) {
    return kintoneClient.record.updateRecord({app: appId, id: recordId, condition: record, withCursor: false});
  }
}

export {
  AppHttpRequestUtils
};